<template>
  <div class="tiezhi-iconselect-modal">
    <div class="modal-title">
      <div class="ul-type">
        <span
          @click="typeName = 'mark'"
          :class="{ active: typeName === 'mark' }"
          title="标记">
          <i class="ri-star-smile-line"></i>
        </span>
        <span
          @click="typeName = 'tiezhi'"
          :class="{ active: typeName === 'tiezhi' }"
          title="贴纸">
          <i class="ri-landscape-line"></i>
        </span>
        <span
          @click="typeName = 'chahua'"
          :class="{ active: typeName === 'chahua' }"
          title="插画">
          <i class="ri-image-edit-line"></i>
        </span>
      </div>
    </div>

    <div class="content" v-show="typeName === 'mark'">
      <div class="button">
        <el-button type="primary" @click="switchLengendShow">{{ visible ? '隐藏图例' : '显示图例' }}</el-button>
      </div>
      <div class="mark-block" v-for="item in markIcons" :key="item.type">
        <p>{{ item.typeName }}</p>
        <div class="mark-list">
          <div
            @click.stop="selectTiezhiIcon({ ...itemName, type: item.type })"
            class="mark-icon-item"
            v-for="itemName in item.icons"
            :key="itemName.icon">
            <svg width="24" height="24">
              <use :xlink:href="`#${itemName.icon}`" />
            </svg>
          </div>
        </div>
      </div>
    </div>
    <div class="content" v-show="typeName === 'tiezhi'">
      <el-scrollbar height="100%">
        <div class="image-marker-list">
          <div
            class="image-marker-list-item"
            v-for="(item, index) in imageMarkers"
            :key="item.name">
            <p>{{ item.name }}</p>
            <div class="list-item_value">
              <div
                v-for="(itemName) in item.list"
                :key="itemName"
                class="image-marker"
                @click.stop="selectTiezhiIcon({ url:itemName, width: 50, height: 50 })">
                <el-image :src="itemName" style="width: 42px; height: 42px;" :lazy="index > 1"></el-image>
              </div>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <div class="content" v-show="typeName === 'chahua'">
      <el-scrollbar height="100%">
        <div class="image-marker-list">
          <div
            class="image-marker-list-item"
            v-for="(item, index) in inbetweeningPictures"
            :key="item.name">
            <p>{{ item.name }}</p>
            <div class="list-item_value chahua">
              <div
                v-for="(itemName) in item.list"
                :key="itemName"
                class="image-marker"
                @click.stop="selectTiezhiIcon({ url:itemName, width: 100, height: 100 })">
                <el-image :src="itemName" style="width: 62px; height: 62px;" :lazy="index > 1"></el-image>
              </div>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue'
import imageMarkers from '../config/imageMarkers'
import inbetweeningPictures from '../config/inbetweeningPictures'
import markIcons from '../config/markIcons'

const props = defineProps({
  lengendVisible: Boolean
})

const emits = defineEmits(['select-tiezhi-icon', 'switch-lengend-show'])
const typeName = ref('mark')

function selectTiezhiIcon (iconItem) {
  emits('select-tiezhi-icon', iconItem)
}

function switchLengendShow () {
  visible.value = !visible.value
  emits('switch-lengend-show', visible.value)
}

const visible = ref(false)
watch(() => props.lengendVisible, newVal => {
  if (newVal === undefined) {
    visible.value = false
  } else {
    visible.value = newVal
  }
}, { immediate: true })
</script>

<style lang="scss">
.tiezhi-iconselect-modal {
  position: fixed;
  top: 180px;
  right: 100px;
  background: #fff;
  width: 290px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 2px 6px 12px 4px rgba(0, 0, 0, 0.1);
  .modal-title {
    display: flex;
    height: 42px;
    border-bottom: 1px solid #f1f1f1;
    justify-content: space-between;
    padding: 0 12px;
    align-items: center;
    .ul-type {
      span {
        display: inline-block;
        padding: 0 20px;
        line-height: 22px;
        border-radius: 4px;
        margin-right: 12px;
        font-size: 0;
        &.active {
          background-color: rgb(242,242,242);
        }
        i {
          color: #000;
          text-align: center;
          font-size: 16px;
          position: relative;
          top: 2px;
        }
      }
      em {
        font-size: 12px;
        color: #999;
        font-weight: normal;
        font-style: normal;
        transform: scale(.8);
        position: relative;
        left: -5px;
        top: 4px;
        display: inline-block;
        letter-spacing: 1px;
      }
    }
  }
  .content {
    height: 600px;
    padding: 20px 0;
    .image-marker-list {
      padding: 0 12px;
      &-item {
        &:not(:last-child) {
          margin-bottom: 20px;
        }
        p {
          font-size: 13px;
          color: #1D1F20;
          font-weight: 600;
          margin-bottom: 10px;
        }
        .list-item_value {
          display: grid;
          grid-template-columns: repeat(5, 1fr);
          gap: 2px;
          &.chahua {
            grid-template-columns: repeat(3, 1fr);
          }
          .image-marker {
            padding: 2px 0;
            border-radius: 4px;
            font-size: 0;
            text-align: center;
            &:hover {
              background: rgb(242,242,242);
            }
          }
        }
      }
    }
    .mark-block {
      margin-bottom: 12px;
      padding: 0 12px;
      p {
        color: #1D1F20;
        font-weight: 600;
        font-size: 13px;
        margin-bottom: 8px;
        user-select: none;
      }
      .mark-list {
        display: flex;
        align-items: flex-start;
        flex-wrap: wrap;
        .mark-icon-item {
          margin: 2px 4px;
        }
      }
    }
    .button {
      padding: 0 12px 16px;
      .el-button {
        display: block;
        width: 100%;
      }
    }
  }
}
</style>
